/*
 * Copyright (c) 2001-2007, TIBCO Software Inc.
 * Use, modification, and distribution subject to terms of license.
 */

jsx3.require("jsx3.gui.Button","jsx3.xml.Cacheable","jsx3.gui.Menu","jsx3.xml.CDF");

/**
 * Provides a object-oriented interface for a button that triggers a drop-down menu.
 * <p/>
 * This class publishes the following model events:
 * <ul>
 * <li><code>EXECUTE</code> - when the user clicks on the enabled button, when the button has focus and the user presses the
 *    space or enter key, or when <code>doExecute()</code> is called on the button.</li>
 * <li><code>MENU</code> - on a mouseup event with the right button when the button has a bound context menu.</li>
 * </ul>
 */
jsx3.Class.defineClass("ux.core.gui.v10.MenuButton", ux.core.gui.v10.Button, [jsx3.xml.Cacheable, jsx3.xml.CDF], function(Button, Button_prototype) {

    /**
     * instance initializer
     * @param strName {String} unique name distinguishing this object from all other JSX GUI objects in the JSX application
     * @param intLeft {int} left position (in pixels) of the object relative to its parent container; not required if button is one of: jsx3.gui.Button.SYSTEMOPEN, jsx3.gui.Button.DIALOGCLOSE, jsx3.gui.Button.DIALOGALPHA, jsx3.gui.Button.DIALOGSHADE
     * @param intTop {int} top position (in pixels) of the object relative to its parent container; not required if button is one of: jsx3.gui.Button.SYSTEMOPEN, jsx3.gui.Button.DIALOGCLOSE, jsx3.gui.Button.DIALOGALPHA, jsx3.gui.Button.DIALOGSHADE
     * @param intWidth {int} width (in pixels) of the object; not required if button is one of: jsx3.gui.Button.SYSTEMOPEN, jsx3.gui.Button.DIALOGCLOSE, jsx3.gui.Button.DIALOGALPHA, jsx3.gui.Button.DIALOGSHADE
     * @param strText {String} text to display in the given button; if null JSXTABLEHEADERCELL.DEFAULTTEXT is used
     */
    Button_prototype.init = function(strName, intLeft, intTop, intWidth, strText) {
        //call constructor for super class
        this.jsxsuper(strName, intLeft, intTop, intWidth, strText);
    };

    Button_prototype.setEnabled = function(intEnabled, bRepaint) {
        this.pressed= false;
//        if(intEnabled == jsx3.gui.Form.STATEENABLED) {
//            this.pressed = false;
//            this.setDynamicProperty("jsxcolor", "@uxcore10@Btn Color");
//            this.setDynamicProperty("jsxborder", "@uxcore10@Btn Border");
//            var arr = new Array(1);
//            arr[0] = "border";
//            this.recalcBox(arr);
//            this.setCSSOverride(ux.core.gui.v10.Button.getUnpressedBG());
//        }
//        else {
//            this.pressed = false;
//            this.setDynamicProperty("jsxcolor", "@uxcore10@Btn Pressed Color");
//            this.setDynamicProperty("jsxborder", "@uxcore10@Btn Disabled Border");
//            var arr = new Array(1);
//            arr[0] = "border";
//            this.recalcBox(arr);
//            this.setCSSOverride(ux.core.gui.v10.Button.getDisabledBG());
//        }
        return this.jsxsuper(intEnabled, bRepaint);
    }

    /**
    * Gets the menu object bound to this button.
    *
    * @return {jsx3.gui.Menu} the menu for this MenuButton
    */
    Button_prototype.getMenu = function() {
        if(!this.childMenu) {
            this.childMenu = new jsx3.gui.Menu(this.getName() + "childMenu", "");
            this.childMenu.myParent = this;
            this.childMenu.getParent = function() {
                return this.myParent;
            }
            var me = this;
            this.childMenu.subscribe(jsx3.gui.Interactive.HIDE, me, me.onMenuHidden);
            this.childMenu.subscribe(jsx3.gui.Interactive.EXECUTE, me, me.onMenuExecute);
            if(this.xmlStr) {
                this.childMenu.setXMLString(this.xmlStr);
            }
            if(this.xmlId) {
                this.childMenu.setXMLId(this.xmlId);
            }
            if(this.xmlURL) {
                this.childMenu.setXMLURL(this.xmlURL);
            }
            if(this.xslStr) {
                this.childMenu.setXSLString(this.xslStr);
            }
            if(this.xslId) {
                this.childMenu.setXSLId(this.xslId);
            }
            if(this.xslURL) {
                this.childMenu.setXSLURL(this.xslURL);
            }
            if(this.xmlTrans) {
                this.childMenu.setXMLTransformers(this.xmlTrans);
            }
            if(this.shareRes) {
                this.childMenu.setShareResources(this.shareRes);
            }
            this.childMenu.setDisplay(jsx3.gui.Block.DISPLAYNONE);
            ux.core.System.getServer().getBodyBlock().setChild(this.childMenu, jsx3.app.Model.PERSISTNONE);
        }
        return this.childMenu;
    }

//    Button.getUnpressedBG = function() {
//        if(!this.unpressedBG) {
//            this.unpressedBG = "background-image: url(" + ux.core.System.getServer().resolveURI("jsxaddin://user!LiveCore/images/matrix/buttonBkgrd.jpg") + "); background-repeat: repeat;";
//        }
//        return this.unpressedBG;
//    }
//
//    Button.getPressedBG = function() {
//        if(!this.pressedBG) {
//            this.pressedBG = "background-image: url(" + ux.core.System.getServer().resolveURI("jsxaddin://user!LiveCore/images/matrix/buttonBkgrd_pressed.jpg") + "); background-repeat: repeat;";
//        }
//        return this.pressedBG;
//    }

    Button.getCustomMethod = function(obj, clazzName, str) {
            for(var f in obj) {
//                jsx3.log("Testing " + f);
                var clazz = jsx3.lang.Class.forName(clazzName);
                var method = clazz.getInstanceMethod(f);
                if(method) {
//                    jsx3.log("     Got method");
                    method = method.getFunction();
//                    jsx3.log("          Function is: " + method);
//                    var strippedMethod = method.toString();
//                    strippedMethod = strippedMethod.replace(String.fromCharCode(32),"");
//if(f == "AN") {
//                    for(var i =0; i<strippedMethod.length; i++) {
//                        jsx3.log("Code: " + strippedMethod.charCodeAt(i));
//                    }
//}
//                    var strippedStr = str.replace(String.fromCharCode(32),"");
//if(f == "AN") {
//                    for(var i = 0; i < strippedStr.length; i++) {
//                        jsx3.log("Search Code: " + strippedStr.charCodeAt(i));
//                    }
//}
//                    jsx3.log("Method: '" + method.toString() + "'");
//                    jsx3.log("Search String: '" + str + "'");
                    if(method.toString().indexOf(str) != -1) {
//                        this.customMethods[clazzName + str] = method;
                        return f;
//                        break;
                    }
                }
            }
//        }
//        return method;
    }

    Button_prototype.onSetParent = function(parent) {
        try {
	    var method = ux.core.gui.v10.MenuButton.getCustomMethod(this, "jsx3.gui.Block", "return this.getText()?this.getText():");
            if(!method) {
                method = ux.core.gui.v10.MenuButton.getCustomMethod(this, "jsx3.gui.Block", "return this.getText() ? this.getText() :");
            }
            if(method) {
//                jsx3.log("Method is: " + method);
            this[method] = function() {
//                jsx3.log("Returning" + (this.getText()?this.getText():"") + " \u25BC");
                return (this.getText()?this.getText():"") + " \u25BC";
            };
            }
            else {
                jsx3.log("Method not found");
            }
        this.pressed = false;
//        this.setCSSOverride(Button.getUnpressedBG());
        this.setText(this.getText())
//            var me = this;
//            this.unsubscribeAll();
//            this.subscribe(jsx3.gui.Interactive.EXECUTE, me, me.onExecute);
        } catch(ex) {
            ux.core.System.logException(ex);
        }
        return this.jsxsuper(parent);
    }

    Button_prototype.onMenuHidden = function(objEvent) {
        this.pressed = false;
        this.setDynamicProperty("jsxcolor", "@uxcore10@Btn Color");
        this.setCSSOverride(ux.core.gui.v10.Button.getUnpressedBG());
        this.repaint();
    }

    /**
     * Executes the specific <code>jsxexecute</code> code for the menu record. This method also fires
     * <code>EXECUTE</code> event for this menu but only under the deprecated 3.0 model event protocol.
     * @param id {String} id for the record whose code will be fired/executed
     * @return {jsx3.gui.Menu} the menu object bound to this MenuButton
     */
    Button_prototype.executeRecord = function(id) {
        this.getMenu().selectItem(id, true);
        var ret = this.getMenu().executeRecord(id);
        var objEvent = new Object();
        var me = this;
        objEvent.context = me;
        objEvent.target = me;
        this.onMenuExecute(objEvent);
        return ret;
    }

    /**
     * Executes the specific <code>jsxexecute</code> code for the menu record. This method also fires
     * <code>EXECUTE</code> event for this menu but only under the deprecated 3.0 model event protocol.
     * @param menuItemName {String} name (jsxtext) for the record whose code will be fired/executed
     * @return {jsx3.gui.Menu} the menu object bound to this MenuButton
     */
    Button_prototype.executeRecordByName = function(menuItemName) {
        var record = this.getMenu().getXML().selectSingleNode("//*[@jsxtext='" + menuItemName + "']");
        var id = record.getAttribute("jsxid");
        return this.executeRecord(id);
    }

    Button_prototype.onMenuExecute = function(objEvent) {
        var msg = new Object();
        msg.subject = jsx3.gui.Interactive.SELECT;
        msg.target = this;
        msg.context = objEvent.context;
        msg.selectedId = objEvent.target.getValue();
        msg.recordNode = objEvent.target.getRecordNode(msg.selectedId);
        this.publish(msg);
    }

    /**
     * Returns the jsxid value in the CDF for the menu item last executed; returns null if unavailable
     * @return {String}
     */
    Button_prototype.getValue = function() {
        return this.getMenu().getValue();
    }

    Button_prototype.getRecordNode = function(strId) {
        return this.getMenu().getRecordNode(strId);
    }

    /**
    * Called on an execute event.  This simulates a button press effect by swapping to the pressed image and back again.
    * The button has no way, currently, of capturing the actual pressed mouse event.  So for now it is done on a click.
    *
    * @param objEvent {Object} the event object that caused this to be fired
    */
    Button_prototype.doPressed = function(objEvent) {
        if(!this.pressed) {
            this.pressed = true;
            this.setDynamicProperty("jsxcolor", "@uxcore10@Btn Pressed Color");
            this.setCSSOverride(ux.core.gui.v10.Button.getPressedBG());
//            this.repaint();
//            this.setCSSOverride(Button.getPressedBG());
        }
        try {
        var root = (jsx3.IDE ? jsx3.IDE : ux.core.System.getServer()).getRootBlock();
        var objGUI = this.getRendered();
        var objRoot = root.getRendered(objGUI);
        var myEvent = jsx3.html.getRelativePosition(objRoot, objGUI);
        myEvent.T = (myEvent.T - 12) + 18;
        myEvent.L = myEvent.L - 14;
        try {
            this.getMenu().showContextMenu(objEvent.context.objEVENT, this, null, myEvent);
        }
        catch(ex) {
            ux.core.System.logException(ex);
        }
        } catch(ex2) {
            ux.core.System.logException(ex2);
        }
        this.repaint();
    };

    Button_prototype.setXMLString = function(str) {
        var ret = this.getMenu().setXMLString(str);
        this.xmlStr = str;
        return ret;
    }

    Button_prototype.setXMLId = function(str) {
        var ret = this.getMenu().setXMLId(str);
        this.xmlId = str;
        return ret;
    }

    Button_prototype.setXMLURL = function(str) {
        var ret = this.getMenu().setXMLURL(str);
        this.xmlURL = str;
        return ret;
    }

    Button_prototype.setXMLTransformers = function(arrTrans) {
        this.xmlTrans = arrTrans != null ? (arrTrans instanceof Array ? arrTrans.join(",") : arrTrans) : null;
        return this.getMenu().setXMLTransformers(this.xmlTrans);
    }

    Button_prototype.getXMLString = function() {
        return this.getMenu().getXMLString();
    }

    Button_prototype.getXMLId = function() {
        return this.getMenu().getXMLId();
    }

    Button_prototype.getXMLURL = function() {
        return this.getMenu().getXMLURL();
    }

    Button_prototype.getXMLTransformers = function() {
        return this.getMenu().getXMLTransformers();
    }

    Button_prototype.setXSLString = function(str) {
        var ret = this.getMenu().setXSLString(str);
        this.xslStr = str;
        return ret;
    }

    Button_prototype.setXSLId = function(str) {
        var ret = this.getMenu().setXSLId(str);
        this.xslId = str;
        return ret;
    }

    Button_prototype.setXSLURL = function(str) {
        var ret = this.getMenu().setXSLURL(str);
        this.xslURL = str;
        return ret;
    }

    Button_prototype.getXSLString = function() {
        return this.getMenu().getXSLString();
    }

    Button_prototype.getXSLId = function() {
        return this.getMenu().getXSLId();
    }

    Button_prototype.getXSLURL = function() {
        return this.getMenu().getXSLURL();
    }

    Button_prototype.setShareResources = function(intShare) {
        var ret = this.getMenu().setShareResources(intShare);
        this.shareRes = intShare;
        return ret;
    }

    Button_prototype.getShareResources = function() {
        return this.getMenu().getShareResources();
    }

    Button_prototype.resetCacheData = function() {
        var ret = this.getMenu().resetCacheData();
        this.getMenu().repaint();
        return ret;
    }

    Button_prototype.clearXmlData = function() {
        return this.getMenu().clearXmlData();
    }

    Button_prototype.getNodeSet = function() {
        return this.getMenu().getNodeSet();
    }

    Button_prototype.getVersion = function() {
        return this.getMenu().getVersion();
    }

    Button_prototype.getXML = function() {
        return this.getMenu().getXML();
    }

    Button_prototype.getXSLParams = function() {
        return this.getMenu().getXSLParams();
    }

    Button_prototype.onDestroyCached = function(objParent) {
        return this.getMenu().onDestroyCached(objParent);
    }

    Button_prototype.removeXSLParam = function(strName) {
        return this.getMenu().removeXSLParam(strName);
    }

    Button_prototype.removeXSLParams = function() {
        return this.getMenu().removeXSLParams();
    }

    Button_prototype.resetData = function() {
        return this.getMenu().resetData();
    }

    Button_prototype.resetXmlCacheData = function() {
        return this.getMenu().resetXmlCacheData();
    }

    Button_prototype.resetXslCacheData = function() {
        return this.getMenu().resetXslCacheData();
    }

    Button_prototype.setNodeSet = function(objNodeSet) {
        return this.getMenu().setNodeSet(objNodeSet);
    }

    Button_prototype.setSourceXML = function(objDoc) {
        return this.getMenu().setSourceXML(objDoc);
    }

    Button_prototype.setXSLParam = function(strName, strValue) {
        return this.getMenu().setXSLParam(strName, strValue);
    }

    Button_prototype.adoptRecord = function(strSourceId, strRecordId, strParentRecordId) {
        return this.getMenu().adoptRecord(strSourceId, strRecordId, strParentRecordId);
    }

    Button_prototype.adoptRecordBefore = function(strSourceId, strRecordId, strSiblingRecordId, bRedraw) {
        return this.getMenu().adoptRecordBefore(strSourceId, strRecordId, strSiblingRecordId, bRedraw);
    }

    Button_prototype.convertProperties = function(objProps, arrProps, bUnion) {
        return this.getMenu().convertProperties(objProps, arrProps, bUnion);
    }

    Button_prototype.deleteRecord = function(strRecordId, bRedraw) {
        return this.getMenu().deleteRecord(strRecordId, bRedraw);
    }

    Button_prototype.deleteRecordProperty = function(strRecordId, strPropName, bRedraw) {
        return this.getMenu().deleteRecordProperty(strRecordId, strPropName, bRedraw);
    }

    Button_prototype.getRecord = function(strRecordId) {
        return this.getMenu().getRecord(strRecordId);
    }

    Button_prototype.getRecordNode = function(strRecordId) {
        return this.getMenu().getRecordNode(strRecordId);
    }

    Button_prototype.insertRecord = function(objRecord, strParentRecordId, bRedraw) {
        return this.getMenu().insertRecord(objRecord, strParentRecordId, bRedraw);
    }

    Button_prototype.insertRecordBefore = function(objRecord, strSiblingRecordId, bRedraw) {
        return this.getMenu().insertRecordBefore(objRecord, strSiblingRecordId, bRedraw);
    }

    Button_prototype.insertRecordNode = function(objRecordNode, strParentRecordId, bRedraw) {
        return this.getMenu().insertRecordNode(objRecordNode, strParentRecordId, bRedraw);
    }

    Button_prototype.insertRecordProperty = function(strRecordId, strPropName, strPropValue, bRedraw) {
        return this.getMenu().insertRecord(strRecordId, strPropName, strPropValue, bRedraw);
    }

    Button_prototype.redrawRecord = function(strRecordId, intAction) {
        return this.getMenu().redrawRecord(strRecordId, intAction);
    }

    Button_prototype.reloadFromSource = function() {
        return this.getMenu().reloadFromSource();
    }

    Button_prototype.clearCachedContent = function() {
        return this.getMenu().clearCachedContent();
    }

    Button_prototype.deselectItem = function(strRecordId) {
        return this.getMenu().deselectItem(strRecordId);
    }

    Button_prototype.disableItem = function(strRecordId) {
        return this.getMenu().disableItem(strRecordId);
    }

    Button_prototype.doValidate = function() {
        return this.getMenu().doValidate();
    }

    Button_prototype.enableItem = function(strRecordId, bEnabled) {
        return this.getMenu().enableItem(strRecordId, bEnabled);
    }

    Button_prototype.getContextParent = function() {
        return this.getMenu().getContextParent();
    }

    Button_prototype.getContextRecordId = function() {
        return this.getMenu().getContextRecordId();
    }

    Button_prototype.getDivider = function() {
        return this.getMenu().getDivider();
    }

    Button_prototype.getXSL = function() {
        return this.getMenu().getXSL();
    }

    Button_prototype.isItemEnabled = function(strRecordId) {
        return this.getMenu().isItemEnabled(strRecordId);
    }

    Button_prototype.redrawRecord = function() {
        return this.getMenu().redrawRecord();
    }

    Button_prototype.selectItem = function(strRecordId, bSelected) {
        return this.getMenu().selectItem(strRecordId, bSelected);
    }

    Button_prototype.setDivider = function(intDivider, bRecalc) {
        return this.getMenu().setDivider(intDivider, bRecalc);
    }

});

if(jsx3.IDE) {
    jsx3.ide.loadTemplateCatalog("prop", "properties/catalog.xml", ux.core.ADDIN);
}
